package com.cisdi.blog.web.admin;

import com.cisdi.blog.entity.Tag;
import com.cisdi.blog.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;

/**
 * 标签
 * @author pengyangyan
 * @version 1.0.0
 * @date 2020年03月19日 11:04:00
 */
@Controller
@RequestMapping("/admin/tags")
public class TagController {

    @Autowired
    TagService tagService;

    @GetMapping
    public String tags(@PageableDefault(size = 10,sort = {"id"},direction = Sort.Direction.DESC) Pageable pageable,
                        Model model){
        model.addAttribute("page",tagService.listTag(pageable));
        return "admin/tags";
    }

    @GetMapping("/input")
    public String tagInput(Model model){
        model.addAttribute("tag",new Tag());
        return "admin/tags_input";
    }

    @GetMapping("/{id}/input")
    public String tagInput(@PathVariable("id") Long id,Model model){
        model.addAttribute("tag",tagService.getTag(id));
        return "admin/tags_input";
    }

    @PostMapping("/post")
    public String addTag(@Valid Tag tag, BindingResult result, RedirectAttributes attributes,Model model){
        Tag tag1 = tagService.findTagByName(tag.getName());
        if (tag1!=null){
            result.rejectValue("name","nameError","标签名重复");
        }
        if (result.hasErrors()){
            model.addAttribute("tag",tag);
            return "admin/tags_input";
        }
        if (tag.getId()==null){
            Tag saveTag = tagService.saveTag(tag);
            if (saveTag==null){
                attributes.addFlashAttribute("message","新增失败");
                attributes.addFlashAttribute("status",false);
            }else {
                attributes.addFlashAttribute("message","新增成功");
                attributes.addFlashAttribute("status",true);
            }
        }else {
            Tag updateTag = tagService.updateTag(tag.getId(), tag);
            if (updateTag==null){
                attributes.addFlashAttribute("message","更新失败");
                attributes.addFlashAttribute("status",false);
            }else {
                attributes.addFlashAttribute("message","更新成功");
                attributes.addFlashAttribute("status",true);
            }
        }

        return "redirect:/admin/tags";

    }

    @GetMapping("/{id}/delete")
    public String delete(@PathVariable Long id,RedirectAttributes attributes){
        tagService.removeTag(id);
        attributes.addFlashAttribute("message","删除成功");
        attributes.addFlashAttribute("status",true);
        return "redirect:/admin/tags";
    }

}
